/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.swta.freezay.database.dbEntities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author Christian
 */
@Entity
@Table(name = "ITEM")
@NamedQueries({@NamedQuery(name = "Item.findAll", query = "SELECT i FROM Item i"), @NamedQuery(name = "Item.findById", query = "SELECT i FROM Item i WHERE i.id = :id"), @NamedQuery(name = "Item.findByPrice", query = "SELECT i FROM Item i WHERE i.price = :price"), @NamedQuery(name = "Item.findByCount", query = "SELECT i FROM Item i WHERE i.count = :count"), @NamedQuery(name = "Item.findByDescription", query = "SELECT i FROM Item i WHERE i.description = :description"), @NamedQuery(name = "Item.findByName", query = "SELECT i FROM Item i WHERE i.name = :name"), @NamedQuery(name = "Item.findByAmount", query = "SELECT i FROM Item i WHERE i.amount = :amount")})
public class Item implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "ID")
    private Integer id;
    @Column(name = "PRICE")
    private Double price;
    @Column(name = "COUNT")
    private Integer count;
    @Column(name = "DESCRIPTION")
    private String description;
    @Column(name = "NAME")
    private String name;
    @Column(name = "AMOUNT")
    private Double amount;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "item", fetch = FetchType.LAZY)
    private List<ItemPackage> itemPackageCollection;
    @JoinColumn(name = "CATEGORY", referencedColumnName = "ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Category category;
    @JoinColumn(name = "UNIT", referencedColumnName = "ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Unit unit;

    public Item() {
        this.name = "";
        this.price = 0d;
        this.amount = 0d;
        this.description = "";
        this.count = 0;
        this.category = new Category();
        this.unit = new Unit();
        itemPackageCollection = new ArrayList<ItemPackage>();
    }

    public Item(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Integer getCount() {
        return count;
    }

    public int getRemainingCount() {
        int remainCount;

        remainCount = this.count.intValue() - this.getMinimumCount();
        
        return remainCount;
    }

    public void setCount(Integer count) {

        if (count >= 0) {
            this.count = count;
        }
        
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public List<ItemPackage> getItemPackageCollection() {
        return itemPackageCollection;
    }

    public void setItemPackageCollection(List<ItemPackage> itemPackageCollection) {
        this.itemPackageCollection = itemPackageCollection;
    }

    public boolean isReserved() {
        boolean reserved = false;

        List<ItemPackage> packages = this.itemPackageCollection;
        int index = 0;

        while(index < packages.size() && !reserved) {
            reserved = packages.get(0).getPackage1().isReserved();
            index++;
        }

        return reserved;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public Unit getUnit() {
        return unit;
    }

    public void setUnit(Unit unit) {
        this.unit = unit;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Item)) {
            return false;
        }
        Item other = (Item) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    public int getMinimumCount() {

        List<ItemPackage> packages = this.itemPackageCollection;
        int minCount = 0;

        for (int i = 0; i < packages.size(); i++) {
            minCount += (packages.get(i).getItemCount()
                        * packages.get(i).getPackage1().getCount().intValue());
        }

        return minCount;
    }

    public void increaseCount() {
        this.count += 1;
    }

    public void decreaseCount() {
        this.count = ((this.count - 1) >= this.getMinimumCount())
                        ? (this.count - 1)
                        : this.count;
    }

    @Override
    public String toString() {
        return "de.swta.freezay.database.dbEntities.Item[id=" + id + "]";
    }

}
